package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "This is an example string"
	exists := strings.Contains(str, "example")
	fmt.Println(exists)

	modify()
	stringAsArrays()
	splitStrings()
	joinanArrayofStrings()
	stringReplaces()
	isHasPrefixOrHasSuffix()
	loopStrings()
	count()
	// 重复几次
	repeatCount()
}

func repeatCount() {
	fmt.Println("ba" + strings.Repeat("na", 2))
	// >> banana
}
func count() {
	ip := "172.16.17.83"
	fmt.Println("ip中有几个.？ ", strings.Count(ip, "."))
}

// 修改
func modify() {
	world := "world"
	tmp := []byte(world)
	tmp[0] = 'W'
	world = string(tmp)
	fmt.Println("world &world", world, &world)
}

func loopStrings() {
	world1 := "世界"
	for i := 0; i < len(world1); i = i + 3 {
		fmt.Print(world1[i : i+3])
	}
	for _, s := range world1 {
		fmt.Printf("%c", s)
	}
}

// String 转数组
func stringAsArrays() {
	str := "abcdefghijklmnopqrstuvwxyz"
	fmt.Println("Chars 3-10: " + str[3:10])

	// printing out first 5 characters
	fmt.Println("First Five: " + str[:5])

	// printing out from 13 to end
	fmt.Println("From 13 on: " + str[13:])
}

// 字符串切割
func splitStrings() {
	sentence := "I'm a sentence     made up of words"
	words := strings.Split(sentence, " ")
	fmt.Printf("%v \n", words)
	words2 := strings.Fields(sentence)
	fmt.Printf("%v \n", words2)
}

func joinanArrayofStrings() {
	lines := []string{"one", "two", "three"}
	str := strings.Join(lines, "$")
	fmt.Println(str)
	//>> one, two, three
}

func stringReplaces() {
	str := "The blue whale loves blue fish."
	newstr := strings.Replace(str, "blue", "red", 1)
	fmt.Println(newstr)
	//>> "The red whale loves blue fish."
	newstr2 := strings.ReplaceAll(str, "blue", "red")
	fmt.Println(newstr2)
	//>> "The red whale loves red fish."

}

func isHasPrefixOrHasSuffix() {
	path := "/home/mkaz"
	isAbsolute := strings.HasPrefix(path, "/")
	fmt.Println(isAbsolute)

	dir := "/home/mkaz"
	hasTrailingSlash := strings.HasSuffix(dir, "/")
	fmt.Println(hasTrailingSlash)

}
